package com.ruoyi.hbase.service;

import com.ruoyi.hbase.config.HbaseTemplate;
import com.ruoyi.hbase.dto.PeopleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class PeopleService {

    @Autowired
    private HbaseTemplate hbaseTemplate;

    public List<PeopleDto> query(String startRow, String stopRow) {
        Map<String, Map<String, String>> userInfoMap =
                this.hbaseTemplate.getResultScanner(
                "userInfo",
                startRow, stopRow);

        List<PeopleDto> list = new ArrayList<>();
        Set<String> set = userInfoMap.keySet();//1001  1002

        set.forEach(keyrow->{
            Map<String, String> stringStringMap = userInfoMap.get(keyrow);

            PeopleDto peopleDto = new PeopleDto();
            peopleDto.setName(stringStringMap.get("name"))
                        .setAge(Integer.parseInt(stringStringMap.get("age")))
            .setAddress(stringStringMap.get("address"));
            list.add(peopleDto);
        });

        return list;
    }

}
